﻿// Type: trx2html.VersionFinder
// Assembly: trx2html, Version=0.7.3.1523, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Users\jigar\Desktop\Vinay\trx2html_0.7\0.7.3\trx2html.exe

using System;
using System.Xml;

namespace trx2html
{
  internal sealed class VersionFinder
  {
    internal SupportedFormats GetFileVersion(string file)
    {
      SupportedFormats supportedFormats = SupportedFormats.unknown;
      using (XmlReader reader = XmlReader.Create(file))
      {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(reader);
        if (this.CheckVersion(xmlDocument.DocumentElement))
          supportedFormats = SupportedFormats.vs2005;
        if (this.IsVS2008(xmlDocument.DocumentElement))
          supportedFormats = SupportedFormats.vs2008;
        if (this.IsVS2010(xmlDocument.DocumentElement))
          supportedFormats = SupportedFormats.vs2010;
      }
      return supportedFormats;
    }

    private bool CheckVersion(XmlElement e)
    {
      try
      {
        if (e.Name == "Tests")
        {
          if (e.ChildNodes[0].Name == "edtdocversion")
          {
            if (Convert.ToInt32(e.ChildNodes[0].Attributes["build"].Value) > 50726)
              return true;
          }
        }
      }
      catch
      {
      }
      return false;
    }

    private bool IsVS2008(XmlElement e)
    {
      try
      {
        if (e.Name == "TestRun")
        {
          if (e.NamespaceURI == "http://microsoft.com/schemas/VisualStudio/TeamTest/2006")
            return true;
        }
      }
      catch
      {
      }
      return false;
    }

    private bool IsVS2010(XmlElement e)
    {
      try
      {
        if (e.Name == "TestRun")
        {
          if (e.NamespaceURI == "http://microsoft.com/schemas/VisualStudio/TeamTest/2010")
            return true;
        }
      }
      catch
      {
      }
      return false;
    }
  }
}
